﻿using ElsProp.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ElsProp
{
    /// <summary>
    /// zhuangBeiBianJi.xaml 的交互逻辑
    /// </summary>
    public partial class zhuangBeiBianJi : Window
    {
        List<zhuangbeilan> zhuangbeilans { get; set; }
        zhuangBeiBianJiMode mode { get; set; }
        public zhuangBeiBianJi()
        {
            InitializeComponent();
            this.Loaded += FristWindow_Loaded;
            zhuangbeilans = new List<zhuangbeilan>();
            mode = this.DataContext as zhuangBeiBianJiMode;
            mode.SuitName = "无";
        }

        private void FristWindow_Loaded(object sender, RoutedEventArgs e)
        {
            foreach (var item in zuo.Children)
            {
                if (item is zhuangbeilan)
                {
                    zhuangbeilans.Add((zhuangbeilan)item);
                }
            }
            foreach (var item in you.Children)
            {
                if (item is zhuangbeilan)
                {
                    zhuangbeilans.Add((zhuangbeilan)item);
                }
            }
            foreach (var item in zhuangbeilans)
            {
                item.click += Item_click;
                item.change += Item_change;
            }
            LoadSuitList();
        }
        private void LoadSuitList()
        {
            if (mode.suitList == null)
            {
                mode.suitList = new();
            }
            mode.suitList.Clear();
            var filse = Directory.GetFiles(Data.path);
            foreach (var file in filse)
            {
                mode.suitList.Add(file);
            }
        }

        private void Item_change(Model.zhuangbeilanmode obj)
        {
            List<PropModel> props = new List<PropModel>();
            string shuxing = "";
            foreach (var item in zhuangbeilans)
            {
                if (item.mode.propModels != null)
                {
                    props.Add(item.mode.propModels);
                }
            }
            mode.SubPropModel(props);
        }

        private void Item_click(Model.zhuangbeilanmode obj, EnumZhuangBeiLan buwei)
        {
            shuxingbianji shuxingbianji = new shuxingbianji();
            if (obj.propModels == null)
            {
                obj.propModels = new Model.PropModel()
                {
                    Suit = string.Empty,
                };
            }
            if (string.IsNullOrEmpty(obj.propModels.guid))
            {
                obj.propModels.guid = Guid.NewGuid().ToString();
            }
            if (obj.propModels != null)
            {
                if (obj.propModels.Properties != null)
                {
                    foreach (var item in obj.propModels.Properties)
                    {
                        item.DeleteThis += shuxingbianji.mode.ItemProperties_DeleteThis;
                        shuxingbianji.mode.shuxingDanjian.Add(item);
                    }
                }
                if (obj.propModels.SuitProperties != null)
                    foreach (var item in obj.propModels.SuitProperties)
                    {
                        item.DeleteThis += shuxingbianji.mode.ItemProperties_DeleteThis;
                        shuxingbianji.mode.shuxingTaoZhuang.Add(item);
                    }

            }
            shuxingbianji.guid = obj.propModels.guid;
            shuxingbianji.DataOut += Shuxingbianji_DataOut;
            shuxingbianji.mode.suitName = mode.SuitName;
            shuxingbianji.mode.danjianName = buwei.ToString();
            shuxingbianji.buwei = buwei;
            shuxingbianji.Show();
        }

        private void Shuxingbianji_DataOut(Model.shuxingbianjiModel obj, string guid)
        {
            if (guid == "")
            {
                return;
            }
            foreach (var item in zhuangbeilans)
            {
                if (item.mode.propModels != null)
                {
                    if (item.mode.propModels.guid == guid)
                    {
                        item.mode.propModels.SuitName = obj.suitItemName;
                        item.mode.propModels.Suit = obj.suitName;
                        item.mode.propModels.Properties = new();
                        item.mode.propModels.SuitProperties = new();
                        foreach (var items in obj.shuxingDanjian)
                        {
                            item.mode.propModels.Properties.Add(items);
                        }
                        foreach (var items in obj.shuxingTaoZhuang)
                        {
                            item.mode.propModels.SuitProperties.Add(items);
                        }
                        item.mode.propModels.Name = obj.danjianName;
                        item.ValueChange();
                        break;
                    }
                }
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            SuitPropModel suitPropModel = new SuitPropModel();
            suitPropModel.PropModels = new List<PropModel>();
            suitPropModel.Suit = mode.SuitName;
            foreach (var item in zhuangbeilans)
            {
                if (item.mode.propModels != null && item.mode.propModels.Properties.Count > 0)
                {
                    if (item.mode.propModels.Properties.Count > 0 || item.mode.propModels.SuitProperties.Count > 0)
                    {
                        if (string.IsNullOrEmpty(item.mode.propModels.SuitName))
                        {
                            if (item.mode.propModels.BuWei == EnumZhuangBeiLan.左上衣 ||
                            item.mode.propModels.BuWei == EnumZhuangBeiLan.左下衣 ||
                            item.mode.propModels.BuWei == EnumZhuangBeiLan.左手套 ||
                            item.mode.propModels.BuWei == EnumZhuangBeiLan.左鞋子
                                )
                            {
                                item.mode.propModels.SuitName = "左边装备";
                            }
                            if (item.mode.propModels.BuWei == EnumZhuangBeiLan.时装上衣 ||
                           item.mode.propModels.BuWei == EnumZhuangBeiLan.时装下衣 ||
                           item.mode.propModels.BuWei == EnumZhuangBeiLan.时装手套 ||
                           item.mode.propModels.BuWei == EnumZhuangBeiLan.时装鞋子 ||
                           item.mode.propModels.BuWei == EnumZhuangBeiLan.时装发型
                               )
                            {
                                item.mode.propModels.SuitName = "左边时装";
                            }
                            if (item.mode.propModels.BuWei == EnumZhuangBeiLan.时装武器 ||
                         item.mode.propModels.BuWei == EnumZhuangBeiLan.时装饰品上衣 ||
                         item.mode.propModels.BuWei == EnumZhuangBeiLan.时装饰品下衣 ||
                         item.mode.propModels.BuWei == EnumZhuangBeiLan.时装饰品耳环 ||
                         item.mode.propModels.BuWei == EnumZhuangBeiLan.时装饰品脸上 ||
                         item.mode.propModels.BuWei == EnumZhuangBeiLan.时装饰品脸下 ||
                         item.mode.propModels.BuWei == EnumZhuangBeiLan.时装饰品脸中 ||
                         item.mode.propModels.BuWei == EnumZhuangBeiLan.时装饰品辅助 ||
                         item.mode.propModels.BuWei == EnumZhuangBeiLan.时装饰品项链 ||
                         item.mode.propModels.BuWei == EnumZhuangBeiLan.时装饰品手臂 ||
                         item.mode.propModels.BuWei == EnumZhuangBeiLan.时装饰品戒指
                             )
                            {
                                item.mode.propModels.SuitName = "右边时装";
                            }
                        }
                        item.mode.propModels.Suit = mode.SuitName;
                        item.mode.propModels.BuWei = item.zhuangBeiLanType;
                        suitPropModel.PropModels.Add(item.mode.propModels);
                    }
                }
            }
            string path = Data.path + "\\" + suitPropModel.Suit + ".json";
            if (!File.Exists(path))
            {
                File.Create(path).Close();
            }
            File.WriteAllText(path, Newtonsoft.Json.JsonConvert.SerializeObject(suitPropModel));
            LoadSuitList();
        }
        private void Clear()
        {
            foreach (var item in zhuangbeilans)
            {
                if (item.mode.propModels != null)
                {
                    item.mode.propModels.guid = "";
                    item.mode.propModels.Suit = "";
                    item.mode.propModels.SuitName = "";
                    item.mode.propModels.Properties.Clear();
                    item.mode.propModels.SuitProperties.Clear();
                    item.mode.propModels.Name = "";
                }
            }
        }
        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Clear();
            if (e.AddedItems.Count > 0)
            {
                var data = File.ReadAllText(e.AddedItems[0].ToString());
                var suit = Newtonsoft.Json.JsonConvert.DeserializeObject<SuitPropModel>(data);
                mode.SuitName = suit.Suit;
                foreach (var item in suit.PropModels)
                {
                    var zb = zhuangbeilans.Find(x => x.zhuangBeiLanType == item.BuWei);
                    if (zb != null)
                    {
                        zb.mode.propModels = item;
                    }
                }
            }
            Item_change(null);
        }
    }
}
